package com.example.forms;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.sale24h.MainActivity;
import com.example.sale24h.MonanActivity;
import com.example.sale24h.R;
import com.example.serverconnect.ITaskListener;
import com.sale24h.common.ICallBack;
import com.sale24h.data.AppData;
import com.sale24h.data.clsBan;
import com.sale24h.data.clsKhuVuc;
import com.sale24h.data.eFRMs;
import com.sale24h.data.adapter.BanAdapter;
import com.sale24h.data.adapter.SpinnerItemAdapter;
import com.sale24h.tasks.GetAllKhuVucTask;

import android.R.integer;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class frmChonBan extends Fragment 
{
	private Spinner cmbKhuVuc;
	private ListView lstvBans;
	private Context context;
	private Menu sysMenu;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		setHasOptionsMenu(true);		
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) 
	{
		View rootview =inflater.inflate(R.layout.frmchonban_layout	, container,false);
		this.context=rootview.getContext();
		
		cmbKhuVuc=(Spinner)rootview.findViewById(R.id.spKhuVuc);
		lstvBans=(ListView)rootview.findViewById(R.id.lstvBans);
		
		cmbKhuVuc.setOnItemSelectedListener(new SpinerItemSelectedClick());
		lstvBans.setOnItemClickListener(new GridViewItemClick());
		
		
		
		LoadData();
		//MainActivity.getInstance().setMenuState(R.id.item_doiban, false);
		return rootview;
	}
	private class GridViewItemClick implements AdapterView.OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			Toast.makeText(context, "OK",200).show();
			
		}				
	}
	private class SpinerItemSelectedClick implements AdapterView.OnItemSelectedListener
	{

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position,
				long id)
		{
			clsKhuVuc kvselected=AppData.getInstance().getKhuVuc(position);
			
			AppData.getInstance().setCurrentKhuVuc(kvselected);
			List<clsBan> lstBans=kvselected.getAllBans();
			
			BanAdapter banAdap=new BanAdapter(context, android.R.layout.simple_list_item_1, lstBans);
			
			banAdap.setCallBack(new ICallBack()
			{
				
				@Override
				public void CallBack(Object obj)
				{
					// implement update action menu when user select item 
					if(obj instanceof List)
					{
						List<Integer> selectedItems=(List<Integer>)obj;
						if(selectedItems.size()>=3)
						{
							//only gop ban
							MenuItem ite_gopban=sysMenu.findItem(R.id.item_gopban);
							ite_gopban.setVisible(true);						
							MenuItem ite_doiban=sysMenu.findItem(R.id.item_doiban);
							ite_doiban.setVisible(false);
							MenuItem ite_tachban=sysMenu.findItem(R.id.item_tachban);
							ite_tachban.setVisible(false);
						}
						else if(selectedItems.size()>=2)
						{
							// gop ban and doi ban
							MenuItem ite_gopban=sysMenu.findItem(R.id.item_gopban);
							ite_gopban.setVisible(true);						
							MenuItem ite_doiban=sysMenu.findItem(R.id.item_doiban);
							ite_doiban.setVisible(true);
							MenuItem ite_tachban=sysMenu.findItem(R.id.item_tachban);
							ite_tachban.setVisible(false);
						}
						else if(selectedItems.size()>=1)
						{
							// only tach ban	
							MenuItem ite_gopban=sysMenu.findItem(R.id.item_gopban);
							ite_gopban.setVisible(false);						
							MenuItem ite_doiban=sysMenu.findItem(R.id.item_doiban);
							ite_doiban.setVisible(false);
							MenuItem ite_tachban=sysMenu.findItem(R.id.item_tachban);
							ite_tachban.setVisible(true);
						}
						else
						{
							// nothing
							MenuItem ite_gopban=sysMenu.findItem(R.id.item_gopban);
							ite_gopban.setVisible(false);						
							MenuItem ite_doiban=sysMenu.findItem(R.id.item_doiban);
							ite_doiban.setVisible(false);
							MenuItem ite_tachban=sysMenu.findItem(R.id.item_tachban);
							ite_tachban.setVisible(false);
						}
					}
					else
					{
						Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
						// item click
						int position=(Integer) obj;
						clsKhuVuc currentKV=AppData.getInstance().getCurrentKhuVuc();
						try
						{
							//set current ban to AppData object to management
							AppData.getInstance().getCurrentKhuVuc().setCurrentBan(currentKV.getBanfromIndex(position));
						}
						catch (Exception e)
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
//						MainActivity.getInstance().gotoView(eFRMs.FRM_PGOIMON);		
						Intent intent=new Intent(context, MonanActivity.class);
						startActivity(intent);
					}
					
					
				}
			});
			
			lstvBans.setAdapter(banAdap);		
			
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0)
		{
			// TODO Auto-generated method stub
			
		}
		
	}

	private void LoadData()
	{
		new GetAllKhuVucTask(new ITaskListener()
		{
			
			@Override
			public void PreRun()
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void Completed(JSONObject result)
			{
				if(result!=null)
				{
					try
					{
						AppData.getInstance().ApplyKhuVuc_BanData(result);
						
						//update data to view
						SpinnerItemAdapter<clsKhuVuc> khuvucadapter=new SpinnerItemAdapter<clsKhuVuc>(context, AppData.getInstance().getAllKhuVucs());
						khuvucadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					
						cmbKhuVuc.setAdapter(khuvucadapter);
					}
					catch (JSONException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
		}, context).execute();
		
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
	{
		this.sysMenu=menu;
		
		inflater.inflate(R.menu.chonban_menu, menu);

        super.onCreateOptionsMenu(menu, inflater);
	}
	
}
